/**
 * 测站类型管理 Controller
 * 
 * 提供测站类型相关的HTTP接口
 * 
 * @author: [开发者名称]
 * @date: [创建日期]
 */

import { Controller, Post, Get, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@midwayjs/swagger';
import { StationTypeService } from '../service/stationType.service';
import {
  CreateStationTypeDTO,
  UpdateStationTypeDTO,
  QueryStationTypeDTO,
  StationTypeResponseDTO,
  PageResultDTO,
  BatchImportStationTypeDTO,
  BatchDeleteDTO,
  CommonResponseDTO
} from '../dto/stationType.dto';

@ApiTags('测站类型管理')
@Controller('/api1/station-type1')
export class StationTypeController {
  @Inject()
  stationTypeService: StationTypeService;

  /**
   * 创建测站类型
   */
  @ApiOperation({ summary: '创建测站类型' })
  @ApiResponse({ status: 200, description: '创建成功', type: CommonResponseDTO })
  @Post('/')
  async create(@Body() createStationTypeDTO: CreateStationTypeDTO): Promise<CommonResponseDTO<StationTypeResponseDTO>> {
    try {
      const result = await this.stationTypeService.create(createStationTypeDTO);
      return {
        code: 200,
        message: '创建成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 400,
        message: error.message || '创建失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 更新测站类型
   */
  @ApiOperation({ summary: '更新测站类型' })
  @ApiParam({ name: 'id', description: '测站类型ID' })
  @ApiResponse({ status: 200, description: '更新成功', type: CommonResponseDTO })
  @Put('/:id')
  async update(
    @Param('id') id: string,
    @Body() updateStationTypeDTO: UpdateStationTypeDTO
  ): Promise<CommonResponseDTO<StationTypeResponseDTO>> {
    try {
      const result = await this.stationTypeService.update(id, updateStationTypeDTO);
      return {
        code: 200,
        message: '更新成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 400,
        message: error.message || '更新失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 删除测站类型
   */
  @ApiOperation({ summary: '删除测站类型' })
  @ApiParam({ name: 'id', description: '测站类型ID' })
  @ApiResponse({ status: 200, description: '删除成功', type: CommonResponseDTO })
  @Del('/:id')
  async delete(@Param('id') id: string): Promise<CommonResponseDTO> {
    try {
      await this.stationTypeService.delete(id);
      return {
        code: 200,
        message: '删除成功',
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 400,
        message: error.message || '删除失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 根据ID查询测站类型
   */
  @ApiOperation({ summary: '根据ID查询测站类型' })
  @ApiParam({ name: 'id', description: '测站类型ID' })
  @ApiResponse({ status: 200, description: '查询成功', type: CommonResponseDTO })
  @Get('/:id')
  async findById(@Param('id') id: string): Promise<CommonResponseDTO<StationTypeResponseDTO>> {
    try {
      const result = await this.stationTypeService.findById(id);
      if (!result) {
        return {
          code: 404,
          message: '测站类型不存在',
          timestamp: Date.now()
        };
      }
      return {
        code: 200,
        message: '查询成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '查询失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 根据编码查询测站类型
   */
  @ApiOperation({ summary: '根据编码查询测站类型' })
  @ApiParam({ name: 'code', description: '测站类型编码' })
  @ApiResponse({ status: 200, description: '查询成功', type: CommonResponseDTO })
  @Get('/code/:code')
  async findByCode(@Param('code') code: string): Promise<CommonResponseDTO<StationTypeResponseDTO>> {
    try {
      const result = await this.stationTypeService.findByCode(code);
      if (!result) {
        return {
          code: 404,
          message: '测站类型不存在',
          timestamp: Date.now()
        };
      }
      return {
        code: 200,
        message: '查询成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '查询失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 分页查询测站类型
   */
  @ApiOperation({ summary: '分页查询测站类型' })
  @ApiResponse({ status: 200, description: '查询成功', type: CommonResponseDTO })
  @Get('/page')
  async findPage(@Query() queryStationTypeDTO: QueryStationTypeDTO): Promise<CommonResponseDTO<PageResultDTO<StationTypeResponseDTO>>> {
    try {
      const result = await this.stationTypeService.findPage(queryStationTypeDTO);
      return {
        code: 200,
        message: '查询成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '查询失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 查询所有测站类型
   */
  @ApiOperation({ summary: '查询所有测站类型' })
  @ApiResponse({ status: 200, description: '查询成功', type: CommonResponseDTO })
  @Get('/list')
  async findAll(): Promise<CommonResponseDTO<StationTypeResponseDTO[]>> {
    try {
      const result = await this.stationTypeService.findAll();
      return {
        code: 200,
        message: '查询成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '查询失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 查询启用的测站类型
   */
  @ApiOperation({ summary: '查询启用的测站类型' })
  @ApiResponse({ status: 200, description: '查询成功', type: CommonResponseDTO })
  @Get('/enabled')
  async findEnabled(): Promise<CommonResponseDTO<StationTypeResponseDTO[]>> {
    try {
      const result = await this.stationTypeService.findEnabled();
      return {
        code: 200,
        message: '查询成功',
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '查询失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 批量导入测站类型
   */
  @ApiOperation({ summary: '批量导入测站类型' })
  @ApiResponse({ status: 200, description: '导入完成', type: CommonResponseDTO })
  @Post('/batch/import')
  async batchImport(@Body() batchImportDTO: BatchImportStationTypeDTO): Promise<CommonResponseDTO> {
    try {
      const result = await this.stationTypeService.batchImport(batchImportDTO);
      return {
        code: 200,
        message: `导入完成，成功：${result.successCount}条，失败：${result.failCount}条`,
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '批量导入失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 批量删除测站类型
   */
  @ApiOperation({ summary: '批量删除测站类型' })
  @ApiResponse({ status: 200, description: '删除完成', type: CommonResponseDTO })
  @Del('/batch')
  async batchDelete(@Body() batchDeleteDTO: BatchDeleteDTO): Promise<CommonResponseDTO> {
    try {
      const result = await this.stationTypeService.batchDelete(batchDeleteDTO);
      return {
        code: 200,
        message: `删除完成，成功：${result.successCount}条，失败：${result.failCount}条`,
        data: result,
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '批量删除失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 更新测站类型状态
   */
  @ApiOperation({ summary: '更新测站类型状态' })
  @ApiParam({ name: 'id', description: '测站类型ID' })
  @ApiResponse({ status: 200, description: '状态更新成功', type: CommonResponseDTO })
  @Put('/:id/status')
  async updateStatus(
    @Param('id') id: string,
    @Body() body: { status: number }
  ): Promise<CommonResponseDTO> {
    try {
      await this.stationTypeService.updateStatus(id, body.status);
      return {
        code: 200,
        message: '状态更新成功',
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 400,
        message: error.message || '状态更新失败',
        timestamp: Date.now()
      };
    }
  }

  /**
   * 检查测站类型编码是否存在
   */
  @ApiOperation({ summary: '检查测站类型编码是否存在' })
  @ApiParam({ name: 'code', description: '测站类型编码' })
  @ApiResponse({ status: 200, description: '检查完成', type: CommonResponseDTO })
  @Get('/check-code/:code')
  async checkCodeExists(
    @Param('code') code: string,
    @Query('excludeId') excludeId?: string
  ): Promise<CommonResponseDTO<{ exists: boolean }>> {
    try {
      const exists = await this.stationTypeService.checkCodeExists(code, excludeId);
      return {
        code: 200,
        message: '检查完成',
        data: { exists },
        timestamp: Date.now()
      };
    } catch (error) {
      return {
        code: 500,
        message: error.message || '检查失败',
        timestamp: Date.now()
      };
    }
  }
}